<?php

/**
 * PassportService.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysmanger\Service;

class PassportService extends BaseService {
    protected $obj_manager_mdl;

    public function __construct()
    {

        parent::__construct();
        $this->obj_manager_mdl = D('manager');
    }

    public function updateManger($mid, $data)
    {

        if (! $data || ! $mid)
        {return false;}
        $where ['mid'] = $mid;
        $this->obj_manager_mdl->where($where)->save($data);
        return true;
    }

    public function checkpwd($request, &$info)
    {

        if (! $request)
        {return false;}
        
        $where = array();
        if (isset($request ['login_name']))
        {
            $where ['user'] = trim($request ['login_name']);
        }
        
        $info = $this->obj_manager_mdl->where($where)->find();
        if (! $info)
        {return false;}
        if($info['disable'])
        {
            throw new \Exception('此账号已被禁用，请联系站点管理员设置');
        }
        // 开始验证
        $decode_pwd = authcode($info ['passwd'], $info ['salt'], 'DECODE');
        if ($decode_pwd == $request ['login_pwd'])
        {return true;}
        
        return false;
    }

    public function savePwd($request, $mid, &$pwd_info)
    {

        if (! $request)
        {return false;}
        
        $this->verifyNewPwd($request ['new_pwd'], $request ['check_new_pwd']);
        
        $where ['mid'] = $mid;
        $manger_info = $this->obj_manager_mdl->where($where)->field('passwd,salt')->find();
        if (! $manger_info)
        {throw new \Exception('管理员记录不存在');}
        
        $rs = $this->verifyPwd($request ['old_pwd'], $manger_info ['passwd'], $manger_info ['salt']);
        if (! $rs)
        {throw new \Exception('原密码错误');}
        
        // 生成密码
        $new_pwd = $request ['new_pwd'];
        $salt = null;
        $this->createPwd($new_pwd, $salt);
        $pwd_info = array(
                'passwd' => $new_pwd, 
                'salt' => $salt 
        );
        // 开始变更数据库
        return $this->obj_manager_mdl->where($where)->save($pwd_info);
    }

}
 